﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using PMS.Core.Utils;
using PMS.Core.Utils.Authentication;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace PMS.Web.Framework.Models
{
    /// <summary>
    /// Represents base Entity model for frontend Model
    /// </summary>
    public partial class BaseEntityModel
    {
        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public BaseEntityModel()
        {
            CustomProperties = new Dictionary<string, object>();
            PostInitialize();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Perform additional actions for binding the model
        /// </summary>
        /// <param name="bindingContext">Model binding context</param>
        /// <remarks>Developers can override this method in custom partial classes in order to add some custom model binding</remarks>
        public virtual void BindModel(ModelBindingContext bindingContext)
        {
        }

        /// <summary>
        /// Perform additional actions for the model initialization
        /// </summary>
        /// <remarks>Developers can override this method in custom partial classes in order to add some custom initialization code to constructors</remarks>
        protected virtual void PostInitialize()
        {
        }

        protected string EncodeId()
        {
            if (Id <= 0) return string.Empty;
            return HashidsHelper.Encode(AuthenticationDefaultConfigs.ItemIdHashSalt, Id);
        }

        protected int DecodeId(string hashId)
        {
            if (string.IsNullOrEmpty(hashId)) return 0;
            return HashidsHelper.Decode(AuthenticationDefaultConfigs.ItemIdHashSalt, hashId);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets model identifier
        /// </summary>
        public virtual int Id { get; set; }

        public string HashId
        {
            get => EncodeId();
            set
            {
                if (Id > 0) return;
                Id = DecodeId(value);
            }
        }
        /// <summary>
        /// Gets or sets property to store any custom values for models 
        /// </summary>
        [XmlIgnore]
        public Dictionary<string, object> CustomProperties { get; set; }

        #endregion

    }

}
